package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.network.TCPClient;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;

public class UserLogin extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private String username;
	private String password;
	private String msg;
	private String result;
	private JFrame JF;

	/**
	 * Launch theapplication.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserLogin frame = new UserLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserLogin() {
		this.JF = this;
		setTitle("中北线在商场-登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);

		textField = new JTextField();
		textField.setBounds(151, 146, 197, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(151, 185, 197, 21);
		contentPane.add(passwordField);

		JButton button = new JButton("登录系统");
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				
				username = textField.getText().trim();
				password = new String(passwordField.getPassword());

				msg = "login###" + username + "###" + password;
				result = new TCPClient().send(msg);

				if ("success".equals(result)) {
					// JOptionPane.showMessageDialog(null,"登录成功！");
					UserStore store = new UserStore(username,null);
					
					UserGoods usergoods = new UserGoods();
					usergoods.setUsername(username);
					JF.dispose();
					textField.setText("");
					passwordField.setText("");
					store.setVisible(true);

				} else if("error".equals(result.split("###")[0])){
					JOptionPane.showMessageDialog(null, result.split("###")[1]);
					textField.setText("");
					passwordField.setText("");
				}else{
					JOptionPane.showMessageDialog(null, result);
					textField.setText("");
					passwordField.setText("");
				}
			}

		});

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(UserLogin.class.getResource("../image/user.jpg")));
		lblNewLabel.setBounds(200, 54, 106, 85);
		contentPane.add(lblNewLabel);
		File dir = new File("D:/store/user");

		if (!dir.exists()) {
			dir.mkdirs();
		}
		JButton registerButton = new JButton("注册");
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				username = textField.getText().trim();
				password = new String(passwordField.getPassword());

				msg = "register###" + username + "###" + password;
				result = new TCPClient().send(msg);

				if ("success".equals(result)) {
					JOptionPane.showMessageDialog(null, "注册成功！");

				} else if ("error".equals(result.split("###")[0])) {
					JOptionPane.showMessageDialog(null, "用户已存在，请重新注册！");
					textField.setText("");
					passwordField.setText("");
				} else {
					JOptionPane.showMessageDialog(null, "注册失败！");
				}

			}
		});
		registerButton.setBounds(151, 216, 93, 23);
		contentPane.add(registerButton);
	}
}
